package com.yuanthink.gwt.client.modules.user.member.views;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.HttpProxy;
import com.extjs.gxt.ui.client.data.JsonPagingLoadResultReader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.yuanthink.gwt.client.modules.user.member.presenters.MemberListPresenter;
/**
 * 
 * @author 06peng
 *
 */
public class MemberListView extends ContentPanel implements MemberListPresenter.Display {

	private Grid<ModelData> grid;
	private BasePagingLoader<PagingLoadResult<ModelData>> loader;
	private ListStore<ModelData> store;
	
	public MemberListView() {
		CheckBoxSelectionModel<ModelData> cbs = new CheckBoxSelectionModel<ModelData>();
		cbs.setSelectionMode(SelectionMode.SINGLE);
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		columns.add(new ColumnConfig("name", "姓名", 80));
		columns.add(new ColumnConfig("userName", "用户名", 80));
		GridCellRenderer<ModelData> sexRenderer = new GridCellRenderer<ModelData>() {
			public String render(ModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<ModelData> store, Grid<ModelData> grid) {
				String val = model.get("sex").toString();
				if ("1.0".equals(val)) {
					return "男";
				} else if ("2.0".equals(val)) {
					return "女";
				} else {
					return "其他";
				}
			}
		};
		ColumnConfig sex = new ColumnConfig("sex", "性别", 60);
		sex.setRenderer(sexRenderer);
		columns.add(sex);
		columns.add(new ColumnConfig("email", "电子邮箱", 150));
		columns.add(new ColumnConfig("phone", "手机号码", 100));
		columns.add(new ColumnConfig("telephone", "电话号码", 100));
		columns.add(new ColumnConfig("province", "省", 80));
		columns.add(new ColumnConfig("city", "城市", 80));
		columns.add(new ColumnConfig("address", "地址", 200));
		ColumnConfig birthday = new ColumnConfig("brithday", "生日", 80);
		birthday.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));  
		columns.add(birthday);
		columns.add(new ColumnConfig("account", "帐号余额", 60));
		
		CheckColumnConfig comment = new CheckColumnConfig("isComment", "是否可以评论", 60);
		columns.add(comment);

		ColumnModel cm = new ColumnModel(columns);

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setTotalName("total");

		String url = "http://www.06peng.com";
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
		builder.setHeader("Content-Type",
				"application/x-www-form-urlencoded; charset=UTF-8");

		HttpProxy<String> proxy = new HttpProxy<String>(builder);
		JsonPagingLoadResultReader<PagingLoadResult<ModelData>> reader = new JsonPagingLoadResultReader<PagingLoadResult<ModelData>>(type);
		loader = new BasePagingLoader<PagingLoadResult<ModelData>>(
				proxy, reader);
		loader.setRemoteSort(true);
		
		final PagingToolBar toolBar = new PagingToolBar(50);  
	    toolBar.bind(loader);  

		store = new ListStore<ModelData>(loader);
		grid = new Grid<ModelData>(store, cm);
		 
		grid.setLoadMask(true);
		grid.setBorders(true);
		grid.setLoadMask(true);
		grid.setAutoExpandColumn("");
		
		this.add(grid);
	}
	
	@Override
	public Grid<ModelData> getGrid() {
		return grid;
	}

}
